package ${nameSpace}.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.cnpc.framework.base.entity.Dict;
import com.cnpc.framework.utils.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.annotation.RefreshCSRFToken;
import com.cnpc.framework.annotation.VerifyCSRFToken;
import com.cnpc.framework.base.pojo.Result;
import ${className};

/**
* ${modelName}管理控制器
* @author jrn
* ${curTime?string("yyyy-MM-dd HH:mm:ss")}由代码生成器自动生成
*/
@Controller
@RequestMapping("/${htmlPrefix}")
public class ${javaPrefix}Controller {

    @Resource
    <#if javaTypes?contains("service")>
    private ${javaPrefix}Service ${htmlPrefix}Service;
    <#else>
    private BaseService baseService;
    </#if>

    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String list(){
        return "${businessPackage}/${htmlPrefix}_list";
    }

    @RefreshCSRFToken
    @RequestMapping(value="/edit",method = RequestMethod.GET)
    public String edit(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "${businessPackage}/${htmlPrefix}_edit";
    }

    @RequestMapping(value="/detail",method = RequestMethod.GET)
    public String detail(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "${businessPackage}/${htmlPrefix}_detail";
    }

    @RequestMapping(value="/get/{id}",method = RequestMethod.POST)
    @ResponseBody
    public ${javaPrefix} get(@PathVariable("id") String id){
        return <#if javaTypes?contains("service")>${htmlPrefix}Service<#else>baseService</#if>.get(${javaPrefix}.class, id);
    }

    @VerifyCSRFToken
    @RequestMapping(value="/save")
    @ResponseBody
    <#assign isObj=1>
    <#list fields as field>
        <#if field.columnName?contains(".")>
            <#assign isObj=0>
        </#if>
    </#list>
    public Result save(<#if isObj=1>${javaPrefix} ${htmlPrefix}<#else>String obj</#if>){
        <#if isObj=0>
        ${javaPrefix} ${htmlPrefix}= JSON.parseObject(obj,${javaPrefix}.class);
        <#list fields as field>
        <#if field.columnName?contains(".")>
            <#assign fieldName=field.columnName?substring(0,field.columnName?index_of("."))>
            <#assign fieldN=fieldName?substring(0,1)?upper_case+fieldName?substring(1)>
        ${htmlPrefix}.set${fieldN}(<#if javaTypes?contains("service")>${htmlPrefix}Service<#else>baseService</#if>.get(${field.type}.class,${htmlPrefix}.get${fieldN}().getId()));
        </#if>
        </#list>
        </#if>
        if(StrUtil.isEmpty(${htmlPrefix}.getId())){
        <#if dateFields?exists&&(dateFields?size>0)>
            <#list dateFields as date>
            ${date};
            </#list>
        </#if>
            <#if javaTypes?contains("service")>${htmlPrefix}Service<#else>baseService</#if>.save(${htmlPrefix});
        }
        else{
        <#if leftFields?exists&&(leftFields?size>0)>
            ${javaPrefix} ${htmlPrefix}_old=<#if javaTypes?contains("service")>${htmlPrefix}Service<#else>baseService</#if>.get(${javaPrefix}.class,${htmlPrefix}.getId());
            BeanUtils.copyProperties(${htmlPrefix},${htmlPrefix}_old, <#list leftFields as str><#if str_index==0>"${str}"<#else>,"${str}"</#if></#list>);
            <#if leftDates?exists&&(leftDates?size>0)>
                <#list leftDates as ld>
             ${ld};
                </#list>
            </#if>
            ${htmlPrefix}_old.setUpdateDateTime(new Date());
            <#if javaTypes?contains("service")>${htmlPrefix}Service<#else>baseService</#if>.update(${htmlPrefix}_old);
            <#else>
            ${htmlPrefix}.setUpdateDateTime(new Date());
            <#if javaTypes?contains("service")>${htmlPrefix}Service<#else>baseService</#if>.update(${htmlPrefix});
            </#if>
        }
        return new Result(true);
    }



    @RequestMapping(value="/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") String id){
        ${javaPrefix} ${htmlPrefix}=this.get(id);
        try{
            baseService.delete(${htmlPrefix});
            return new Result();
        }
        catch(Exception e){
            return new Result(false,"该数据已经被引用，不可删除");
        }
    }



}
